﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JobSeek.Classes;
using System.Web.Security;
using System.Web.Services;

namespace JobSeek.Poster
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnContinue_Click(object sender, EventArgs e)
        {
            //Register complete, proceed to payment.
            //Page will be valid as all validations are done of client side. Do not need to check this unless we are using recaptcha again

            //Create Advertiser Object
            if (Page.IsValid)
            {
                JobSeek.Advertiser advertiser = new JobSeek.Advertiser();

                advertiser.username = tbxUsername.Text;
                advertiser.password = tbxPassword.Text;

                advertiser.firstname = tbxContactFirstName.Text;
                advertiser.lastname = tbxContactLastName.Text;
                advertiser.email = tbxCoyEmail.Text;
                advertiser.email_for_applicant = tbxAppEmail.Text;
                advertiser.main_contact = tbxPrimaryContact.Text;
                advertiser.secondary_contact = String.IsNullOrEmpty(tbxSecondaryContact.Text) ? "" : tbxSecondaryContact.Text;
                advertiser.fax = String.IsNullOrEmpty(tbxFax.Text) ? null : tbxFax.Text;

                advertiser.company_name = tbxCoyName.Text;
                advertiser.postal_address = tbxPostalAdd1.Text;
                advertiser.postal_address_2 = String.IsNullOrEmpty(tbxPostalAdd2.Text) ? "" : tbxPostalAdd2.Text;
                advertiser.country = ddlCountries.SelectedValue;
                advertiser.suburb = String.IsNullOrEmpty(tbxSuburb.Text) ? "" : tbxSuburb.Text;
                advertiser.num_of_emp = ddlNumEmp.SelectedValue;
                advertiser.emp_type = ddlIndustry.SelectedValue;
                //need to add city for advertiser tomorrow;

                if (Session["jobAds"] != null)
                {
                    Job jobAds = (Job)Session["jobAds"];
                    //link jobAds with advertiser
                    advertiser.Jobs.Add(jobAds);
                }

                if (AdvertiserMembershipProvider.CreateAdvertiser(advertiser))
                {
                    //add user to role
                    Roles.AddUserToRole(advertiser.username, "Advertiser");
                    //go to billing
                }
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //cancel and remove all sessions pertaining to the registration. Redirect to the Advertiser promotion page
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            //go back to the previous form. Having problems with the logo part though.
        }

        [WebMethod]
        public static bool CheckUsername(string username)
        {
            return AdvertiserMembershipProvider.CheckAdvertiser(username);
        }

        protected void DuplicateUsernameValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (AdvertiserMembershipProvider.CheckAdvertiser(tbxUsername.Text))
            {
                //available
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
                //not available
            }
        }
    }
}